import Mock from 'mockjs';
import menuCnData from './data/cnmenu.json'
import menuEnData from './data/enmenu.json'
import dataProviderFactory from './dataProvider/index.js'

let isMock = false;
var mockData = {
    // 用户数据模拟
    users: [Mock.mock({
            keyID: 1000,
            loginName: "xufeng",
            pwd: "1",
            groupCode: "Center",
            groupType: "Center",
            groupName: "总部",
            name: "许峰",
            email: "446187489@qq.com",
            userType: "",
            tokenCode: Mock.mock('@guid()'),
            language: "",
            appType: "",
        }),
        Mock.mock({
            keyID: 1001,
            loginName: "admin",
            pwd: "1",
            groupCode: "Center",
            groupType: "Center",
            groupName: "总部",
            name: "管理员",
            email: "admin@qq.com",
            userType: "",
            tokenCode: Mock.mock('@guid()'),
            language: "",
            appType: "",
        }),
    ],


    // 模拟登陆
    userLogin: function userLogin(options) {
        console.log('mock userLogin');
        var result = {
            isSuccess: "Y",
            reason: "",
            message: [],
            data: {}
        };
        var postData = JSON.parse(options.body);
        for (var i = 0; i < mockData.users.length; i++) {
            var user = mockData.users[i];
            if (postData.loginName == user.loginName && postData.pwd == user.pwd) {
                user.language = postData.language;
                user.appType = postData.appType;
                result.data = user;
                console.log('userLoginOK');
                return result;
            }
        }
        result.isSuccess = 'N';
        result.reason = 'userNameOrPwdError';
        return result;
    },
    // 获得用户菜单
    queryUserMenu: function queryUserMenu(options) {
        console.log('mock queryUserMenu');
        var postData = JSON.parse(options.body);
        var result = {
            isSuccess: "Y",
            reason: "",
            message: [],
            data: { queryResult: [] }
        };
        // 通过语言来加载不同的菜单json文件
        if (postData.language == 'cn') {
            result.data.queryResult = menuCnData.menus;
        } else {
            result.data.queryResult = menuEnData.menus;
        }

        return result;
    },
    // 模拟查询
    queryModel: function queryModel(options) {
        console.log('mock queryModel');
        var postData = JSON.parse(options.body);

        var dataProvider = dataProviderFactory.factory(postData);
        if (dataProvider == null) {
            return {
                isSuccess: "N",
                reason: "mockDataProviderNoFind",
                message: [postData.className],
                data: null
            };
        }
        return dataProvider.queryData(postData);
    },

    procCommand: function procCommand(options) {
        console.log('mock procCommand');
        const postData = JSON.parse(options.body);
        const result = {
            isSuccess: "Y",
            reason: "",
            message: [],
            data: {}
        };
        return result;
    },

    queryDomainModelByKeyID: function queryDomainModelByKeyID(options) {
        console.log('mock queryDomainModelByKeyID');
        var postData = JSON.parse(options.body);
        var dataProvider = dataProviderFactory.factory(postData);
        if (dataProvider == null) {
            return {
                isSuccess: "N",
                reason: "mockDataProviderNoFind",
                message: [postData.className],
                data: null
            };
        }
        return dataProvider.queryByKeyID(postData);
    }
};

if (isMock) {
    Mock.mock('/api/UserLogin', 'post', mockData.userLogin);
    Mock.mock('/api/QueryUserMenu', 'post', mockData.queryUserMenu);
    Mock.mock('/api/QueryModel', 'post', mockData.queryModel);
    Mock.mock('/api/ProcCommand', 'post', mockData.procCommand);
    Mock.mock('/api/QueryDomainModelByKeyID', 'post', mockData.queryDomainModelByKeyID);
}
export default mockData;